package
{
	import as3isolib.core.IIsoDisplayObject;
	import as3isolib.data.INode;
	import as3isolib.display.IsoSprite;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.Pt;
	import as3isolib.utils.IsoDrawingUtil;
	
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.utils.Dictionary;
	
	import mx.core.mx_internal;

	public class PathManager
	{
		public static const VECTOR_LENGTH:Number = .5 * Constants.SQUARE_SIZE;
		
		private var m_previewPathVector:PathVector;
		private var m_previewPathINode:INode;
		private var m_debugSceneEnabled:Boolean = false;
		private var m_pathScene:IsoScene = new IsoScene();
		
		private var m_pathLinks:Vector.<PathLink> = new Vector.<PathLink>;
		private var m_pathNodes:Vector.<PathNode> = new Vector.<PathNode>;
		private var m_highlitedNode:PathNode;
		
		public function PathManager()
		{
		}
		
		public function findNode(pos:Vector3D):PathNode{
			var i:int = 0, length:int = m_pathNodes.length;
			for(i = 0; i < length; i++){
				if(m_pathNodes[i].v3.subtract(pos).length == 0){
					return m_pathNodes[i];
				}
			}
			
			return null;
		}

		public function addLink(pathLink:PathLink):void{
			m_pathLinks.push(pathLink);
			m_pathScene.addChild(pathLink.displayObject);
		}
		
		
		public function highlightNode(pathNode:PathNode):void{
			m_highlitedNode = pathNode;
			pathNode.displayObject.container.filters = [Global.highlightFilter];
		}
		
		public function removeHighlight():void{
			if(m_highlitedNode != null){
				m_highlitedNode.displayObject.container.filters = [];
				m_highlitedNode = null;
			}
		}
		
		public function getNextDestination(pathingObject:IPathingObject):Vector3D{
			var nextDestination:Vector3D;
			var lastDestination:Vector3D = pathingObject.getLastDestination();
			var random:int;
			
			if(lastDestination != null && m_pathLinks.length){ 
				var pathNode:PathNode = findNode(lastDestination);
				if(pathNode){
					var pathLinks:Vector.<PathLink> = getPathLinksByStartingNode(pathNode);
					if(pathLinks.length){
						random = Math.floor(Math.random()*(pathLinks.length));
						nextDestination = pathLinks[random].endNode.v3;
					}
				}
			}
			
			if(nextDestination == null){
				random = Math.round(Math.random()*(m_pathNodes.length-1));
				nextDestination = m_pathNodes[random].v3;
			} 
			
			return nextDestination;
		}
		
		private function getPathLinksByStartingNode(pathNode:PathNode):Vector.<PathLink>{
			var i:int = 0, length:int = m_pathLinks.length;
			var pathLinks:Vector.<PathLink> = new Vector.<PathLink>;
			for(i = 0; i < length; i++){
				if(m_pathLinks[i].startNode == pathNode){
					pathLinks.push(m_pathLinks[i]);	
				}
			}
			
			return pathLinks;
		}
		
		
		public function addNode(pathNode:PathNode):void{
			if(this.findNode(pathNode.v3) == null){
				m_pathNodes.push(pathNode);
				m_pathScene.addChild(pathNode.displayObject);
			}
			
			Global.changeCitizenPathingStrategy();
		}
		
		
		public function showDebugRendering():void{
			if(!m_debugSceneEnabled){
				m_debugSceneEnabled = true;
				Global.isoView.addScene(m_pathScene);
				Global.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		
		public function disableDebugRendering():void{
			if(m_debugSceneEnabled){
				m_debugSceneEnabled = false;
				Global.isoView.removeScene(m_pathScene);
				Global.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}			
		}
		
		private function onEnterFrame(e:Event):void{
			m_pathScene.render();
		}
	}
}